'''
# @Title:
# @Time : 2022/3/14 15:47
# @File : JsonSchema_Demo.py
# @Software: PyCharm

'''
from jsonschema import validate

json_data = {
    "category_list":{
        "can_creat_category":True,
        "total_num" :199,
        "categories": [
            {
                "goods_name":"商品1",
                "price":11,
                "sale_num":100,
                "description":"描述1",
            },
            {
                "goods_name": "商品2",
                "price": 22,
                "sale_num": 200,
                "description": "描述2",
            },
            {
                "goods_name": "商品3",
                "price": 33,
                "sale_num": 300,
                "description": "描述3",
            }
        ]
    }
}


json_schema = {
    "type":"object",
    "required":["category_list"],
    "properties":{
        "category_list":{
            "type":"object",
            "required":["can_creat_category","total_num","categories"],
            "properties":{
                "can_creat_category":{
                    "type":"boolean"
                },
                "total_num":{
                    "type":"integer",
                    "minimum":0
                },
                "categories":{
                    "type":"array",
                    "required":["goods_name","price","sale_num","description"],
                    "properties":{
                        "goods_name":{
                            "type":"string",
                            "maxLength":127
                        },
                        "price":{
                            "type":"number",
                            "minium":0
                        },
                        "sale_num":{
                            "type":"integer",
                            "minium":0
                        },
                        "description":{
                            "type":"string",
                            "maxLength":127
                        }
                    }
                }
            }
        }
    }
}

print(validate(instance=json_data, schema=json_schema))





